package com.xgst.bmg.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;

/**
 * @BelongsProject: screen-win
 * @BelongsPackage: com.xgst.bmg.utils
 * @Author: xgstjs-11
 * @CreateTime: 2021-01-22 18:58:18
 * @Description: 本机信息获取工具类
 */
public class LocalInforUtils {

    //获取本机MAC地址
    public static String getLocalMac() throws UnknownHostException, SocketException {
        InetAddress ia = InetAddress.getLocalHost();
        //获取网卡，获取地址
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
        StringBuffer sb = new StringBuffer("");
        for(int i=0; i<mac.length; i++) {
            if(i!=0) {
                sb.append("-");
            }
            //字节转换为整数
            int temp = mac[i]&0xff;
            String str = Integer.toHexString(temp);
            if(str.length()==1) {
                sb.append("0"+str);
            }else {
                sb.append(str);
            }
        }
        return sb.toString().toUpperCase();
    }

    //读取本地配置文件screenwinipconfiguration
    public static JSONObject getConfFile() throws IOException {
        File file = new File("C:\\screenwinipconfiguration");
        if(!file.exists()){
            return null;
        }
        FileInputStream inputStream = new FileInputStream(file);
        int length = inputStream.available();
        byte bytes[] = new byte[length];
        inputStream.read(bytes);
        inputStream.close();
        String str =new String(bytes, StandardCharsets.UTF_8);
        JSONObject strJson = JSONObject.parseObject(str);
        return strJson;
    }

}
